<?php
class Boisson extends Produit{
	public $contenance;
	public function __construct($id, $nom, $prix,$contenance){
		parent::__construct($id, $nom, $prix);
		$this->contenance=$contenance;
	}
	
	public function save()
	{
		$query = "";
		// New pizza
		if(empty($this->id) || ($this->id == 0))
		{
			$query = "INSERT INTO boissons(boisson_volume, boisson_nom, boisson_prix)
			    			VALUES ('".$this->contenance."', '".$this->nom."', '".$this->prix."')";		
		}
		else
		{
			$query = "UPDATE boissons
   SET boisson_volume='".$this->contenance."', boisson_nom='".$this->nom."', boisson_prix='".$this->prix."'
 				WHERE boisson_id='".$this->id."'";
		}
		DbConnector::get()->exec($query);
	}
	
	// Gets all beverages
	public static function getAll()
	{
		$boissons = array();
		$query = 'SELECT boisson_id, boisson_volume, boisson_nom, boisson_prix FROM boissons;';
		$stmt = DbConnector::get()->prepare($query);
		$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
		// Instanciates each beverage
		foreach($result as $drink)
		{
			$boissons[] = new Boisson($drink['boisson_id'], $drink['boisson_nom'], $drink['boisson_prix'], $drink['boisson_volume']);
		}
		// Returns array
	}
	
	public function  getXML(){
		$xml = new DOMDocument;
		$xml->formatOutput = true;
		
		$boisson = $xml->createElement('boisson');
		$boissonNode=$xml->appendChild($boisson);
		$boissonNode->setAttribute('id',$this->id);
		$boissonNode->appendChild(new DOMElement('nom',$this->nom));
		$boissonNode->appendChild(new DOMElement('prix',$this->prix));
		$boissonNode->appendChild(new DOMElement('contenance',$this->contenance));
		return $xml->getElementsByTagName('boisson')->item(0);
	}
}